//
//  ZLColorSlider.m
//  ZLColorWall
//
//  Created by 赵振磊 on 2024/3/16.
//

#import "ZLColorSlider.h"

@interface ZLColorSlider()
/// 格子
@property (nonatomic, strong) CAShapeLayer *boardLayer;
/// 当前色
@property (nonatomic, strong) CAGradientLayer *colorLayer;
/// 选中背景色背景
@property (nonatomic, strong) UIView *selBgView;
/// 选中背景色
@property (nonatomic, strong) UIView *selTopView;
@end

@implementation ZLColorSlider

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.backgroundColor = [UIColor whiteColor];
        [self.layer addSublayer:self.boardLayer];
        [self.layer addSublayer:self.colorLayer];
        self.layer.cornerRadius = self.frame.size.height / 2.0;
        self.layer.masksToBounds = YES;
        
        [self addSubview:self.selBgView];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [self addGestureRecognizer:pan];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:self];
    point.y = self.selBgView.center.y;
    point.x = MAX(MIN(self.frame.size.width - 17, point.x), 17);
    self.selBgView.center = point;
    [self updateAlpha];
}

- (void)setColor:(UIColor *)color {
    _color = color;
    self.selTopView.backgroundColor = color;
    self.colorLayer.colors = @[(id)[UIColor clearColor].CGColor, (id)color.CGColor];
}

#pragma mark - action

- (void)panAction:(UIPanGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self];
    point.x = MAX(MIN(self.frame.size.width - 17, point.x), 17);
    point.y = self.selBgView.center.y;
    self.selBgView.center = point;
    [gesture setTranslation:CGPointZero inView:self];
    [self updateAlpha];
}

- (void)updateAlpha {
    CGFloat all = self.frame.size.width - 34;
    CGFloat scale = (self.selBgView.center.x - 17) / all;
    self.selTopView.alpha = scale;
    if (self.alphaChange) {
        self.alphaChange(scale);
    }
}

#pragma mark - init

- (CAShapeLayer *)boardLayer {
    if (!_boardLayer) {
        CGFloat side = self.frame.size.height / 3.0;
        NSInteger rows = self.frame.size.height / side;
        NSInteger columns = self.frame.size.width / side;
        
        // 创建格子形状
        UIBezierPath *blackPath = [UIBezierPath bezierPath];
        for (NSInteger row = 0; row < rows; row++) {
            for (NSInteger col = 0; col < columns; col++) {
                CGRect cellRect = CGRectMake(col * side, row * side, side, side);
                UIBezierPath *subPath = [UIBezierPath bezierPathWithRect:cellRect];
                if ((row + col) % 2 == 0) {
                    [blackPath appendPath:subPath];
                }
            }
        }
        
        _boardLayer = [CAShapeLayer layer];
        _boardLayer.path = blackPath.CGPath;
        _boardLayer.fillColor = [UIColor grayColor].CGColor;
        _boardLayer.frame = self.bounds;
    }
    return _boardLayer;
}

- (CAGradientLayer *)colorLayer {
    if (!_colorLayer) {
        _colorLayer = [CAGradientLayer layer];
        _colorLayer.colors = @[(id)[UIColor colorWithWhite:1 alpha:0.0].CGColor,
                               (id)[UIColor colorWithWhite:1 alpha:1.0].CGColor];
        _colorLayer.locations = @[@0.0, @1.0];
        _colorLayer.startPoint = CGPointMake(0.0, 0.0);
        _colorLayer.endPoint = CGPointMake(1.0, 0.0);
        _colorLayer.frame = self.bounds;
    }
    return _colorLayer;
}

- (UIView *)selBgView {
    if (!_selBgView) {
        _selBgView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - 32, 2, 30, 30)];
        _selBgView.backgroundColor = [UIColor whiteColor];
        _selBgView.layer.cornerRadius = 15;
        _selBgView.layer.shadowColor = [UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:0.1500].CGColor;
        _selBgView.layer.shadowOffset = CGSizeMake(0, 3);
        _selBgView.layer.shadowOpacity = 1;
        _selBgView.layer.shadowRadius = 7;
        [_selBgView addSubview:self.selTopView];
    }
    return _selBgView;
}

- (UIView *)selTopView {
    if (!_selTopView) {
        _selTopView = [[UIView alloc] initWithFrame:CGRectMake(3, 3, 24, 24)];
        _selTopView.backgroundColor = [UIColor whiteColor];
        _selTopView.layer.cornerRadius = 12;
        _selTopView.layer.shadowColor = [UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:0.1500].CGColor;
        _selTopView.layer.shadowOffset = CGSizeMake(0, 3);
        _selTopView.layer.shadowOpacity = 1;
        _selTopView.layer.shadowRadius = 7;
    }
    return _selTopView;
}

@end
